package com.guoguo.leetcode.character;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestAir {
    /**
     * 测试
     */
    public static void main(String[] args) throws Exception {

        /**
         * Case 1
         * 输出：0.8163
         */
        Map<String, Integer> map1 = new HashMap<>();
        map1.put("T3", 3000);
        double engineSpeedAverageConversionValue1 = getEngineSpeedAverageConversionValue(map1);
        System.out.println("Case1 结果 "+getAirHealthVal(engineSpeedAverageConversionValue1,"优", "15", "26"));

        /**
         * Case 2
         * 正确答案应该是：0.8163；你们给的结果1.2245是错误的。
         *  描述： [ 给的面试题中第二题的输出结果给错了，T5-1000对应的转换值应该是20，正确的输出结果0.8163；
         *  你们应该是错看成了T3-1000，如果是T3-1000的话输出的结果才是1.2245 ]
         */
        Map<String, Integer> map2 = new HashMap<>();
        map2.put("T5", 1000);
        double engineSpeedAverageConversionValue2 = getEngineSpeedAverageConversionValue(map2);
        System.out.println("Case2 结果 "+getAirHealthVal(engineSpeedAverageConversionValue2,"差", "1.5", "26") + "。你们给的结果1.2245是错误的");

        /**
         * Case 3
         * 输出：1.125
         */
        Map<String, Integer> map3 = new HashMap<>();
        map3.put("T3", 2000); // 转速转换值30
        map3.put("T5", 8000); // 转速转换值60
        double engineSpeedAverageConversionValue3 = getEngineSpeedAverageConversionValue(map3);
        System.out.println("Case3 结果 "+getAirHealthVal(engineSpeedAverageConversionValue3,"中", "10", "30"));
    }

    enum AirQualityEnum {
        EXCELLENT("优", 4),
        GOOD("良", 3),
        MEDIUM("中",2),
        POOR("差",1);

        private String desc;
        private int val;

        public String getDesc(){
            return desc;
        }
        public int getVal(){
            return val;
        }

        AirQualityEnum(String desc, int val) {
            this.desc = desc;
            this.val = val;
        }

        public static AirQualityEnum match(String key) {
            AirQualityEnum result = null;
            for (AirQualityEnum airQualityEnum : values()) {
                if (airQualityEnum.getDesc().equals(key)) {
                    result = airQualityEnum;
                    break;
                }
            }
            return result;
        }

    }

    /**
     * @desc 计算天气质量转换值
     */
    public static int getWeatherQualityConversionValue(String weatherDesc) throws Exception {
        try{
             return AirQualityEnum.match(weatherDesc).getVal();
        }catch (Exception e){
            throw new Exception("请输入正确的天气质量描述");
        }
    }

    /**
     * @desc 计算温度差
     */
    public static float getTemperatureDifference(String airOutTemperatureStr, String airInTemperatureStr) throws Exception {
        float airOutTemperature = Float.valueOf(airOutTemperatureStr);
        float airInTemperature = Float.valueOf(airInTemperatureStr);
        return airOutTemperature - airInTemperature;
    }

    /**
     * @desc ⻜机健康归⼀化公式计算
     * @param engineSpeedAverageConversionValue 发动机转速平均值
     * @param airInTemperature      飞机内部温度
     * @param airOutTemperature     飞机外部温度
     */
    public static double getAirHealthVal(double engineSpeedAverageConversionValue, String weatherDesc, String airOutTemperature, String airInTemperature) throws Exception {
        double healthVal = 0;
        int weatherQualityConversionValue = getWeatherQualityConversionValue(weatherDesc);
        float temperatureDifference = getTemperatureDifference(airOutTemperature, airInTemperature);

        BigDecimal b = new BigDecimal(engineSpeedAverageConversionValue / (temperatureDifference * weatherQualityConversionValue));
        healthVal = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        return Math.abs(healthVal);
    }

    /**
     * 获取平均转速值
     * @param 【map】key-发动机型号;value-发动机转速
     */
    public static double getEngineSpeedAverageConversionValue(Map<String, Integer> map){
        List<Integer> tempArr = new ArrayList<>();
        for (String key : map.keySet()) {
            tempArr.add(getEngineSpeedConversionValue(key, map.get(key)));
        }
        double sum = 0;
        for(Integer item : tempArr){
            sum+=item;
        }
        return sum / tempArr.size();
    }

    /**
     * @desc 根据发动机型号和转速计算转换值
     * @param engineModel 发动机型号
     * @param speed 发动机转速
     */
    public static int getEngineSpeedConversionValue(String engineModel, int speed){
       if(engineModel.equals("T3")){
          if(speed < 3000){
            return 30;
          }else if(speed >= 3000 && speed <=5000){
               return 60;
           }else{
               return 100;
           }
       }
        if(engineModel.equals("T5")){
            if(speed < 3000){
                return 20;
            }else if(speed >= 3000 && speed <=5000){
                return 40;
            }else{
                return 60;
            }
        }
        if(engineModel.equals("E6")){
            if(speed < 3000){
                return 10;
            }else if(speed >= 3000 && speed <=5000){
                return 20;
            }else{
                return 50;
            }
        }
        return 0;
    }


}
